package cn.iocoder.yudao.module.project.service.defectiveproductrecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.defectiveproductrecord.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.defectiveproductrecord.DefectiveProductRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.defectiveproductrecord.DefectiveProductRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 不良品记录 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DefectiveProductRecordServiceImpl implements DefectiveProductRecordService {

    @Resource
    private DefectiveProductRecordMapper defectiveProductRecordMapper;

    @Override
    public String createDefectiveProductRecord(DefectiveProductRecordSaveReqVO createReqVO) {
        // 插入
        DefectiveProductRecordDO defectiveProductRecord = BeanUtils.toBean(createReqVO, DefectiveProductRecordDO.class);
        defectiveProductRecordMapper.insert(defectiveProductRecord);
        // 返回
        return defectiveProductRecord.getId();
    }

    @Override
    public void updateDefectiveProductRecord(DefectiveProductRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateDefectiveProductRecordExists(updateReqVO.getId());
        // 更新
        DefectiveProductRecordDO updateObj = BeanUtils.toBean(updateReqVO, DefectiveProductRecordDO.class);
        defectiveProductRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteDefectiveProductRecord(String id) {
        // 校验存在
        validateDefectiveProductRecordExists(id);
        // 删除
        defectiveProductRecordMapper.deleteById(id);
    }

    private void validateDefectiveProductRecordExists(String id) {
        if (defectiveProductRecordMapper.selectById(id) == null) {
            throw exception(DEFECTIVE_PRODUCT_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public DefectiveProductRecordDO getDefectiveProductRecord(String id) {
        return defectiveProductRecordMapper.selectById(id);
    }

    @Override
    public PageResult<DefectiveProductRecordDO> getDefectiveProductRecordPage(DefectiveProductRecordPageReqVO pageReqVO) {
        return defectiveProductRecordMapper.selectPage(pageReqVO);
    }

}